package com.local.admin.controller;

import com.local.admin.constants.AdminConstants;
import com.local.admin.service.CarouselService;
import com.local.carousel.feign.CarouselServiceFeign;
import com.local.carousel.pojo.Carousel;
import com.local.carousel.pojo.CarouselDeleteRequestParam;
import com.local.carousel.pojo.CarouselListRequestParam;
import com.local.common.constants.ExceptionCodeEnum;
import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Objects;

/**
 * 轮播图控制器
 */

@Controller
@RequestMapping("/admin/carousel")
public class AdminCarouselController {

    @Autowired
    private CarouselServiceFeign carouselServiceFeign;

    @Autowired
    private CarouselService carouselService;

    /**
     * 轮播图的列表页面
     * @return
     */
    @GetMapping({"", "/", "/index"})
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "carousel");
        return "admin/carousel";
    }

    /**
     * 轮播图列表页的数据，通过远程调用carousel_service_openfeign接口方法获取数据
     * @param params Map<String, Object> limit: 10, page: 1, order: asc, sidx: carouselRank(排序字段，默认为空)
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestParam Map<String, Object> params) {
        Object param_page = params.get("page");
        Object param_limit = params.get("limit");
        if (Objects.isNull(param_page) || param_page.toString().isEmpty() || Objects.isNull(param_limit) || param_limit.toString().isEmpty()) {
            return new ResultUtil(ExceptionCodeEnum.BUSINESS_EXCEPTION.getCode(), "参数异常");
        }
        Integer page = Integer.valueOf(param_page.toString());
        Integer limit = Integer.valueOf(param_limit.toString());
        String sort = params.get("sidx").toString().isEmpty() ? "carousel_id" : params.get("sidx").toString();
        String order = params.get("order").toString().isEmpty() ? "asc" : params.get("order").toString();

        //请求参数
        CarouselListRequestParam carouselListRequestParam = new CarouselListRequestParam(page, limit, sort, order, -1);
        //ResultUtil list = carouselServiceFeign.list(carouselListRequestParam);
        ResultUtil<PageResultUtil<Carousel>> list = carouselService.list(carouselListRequestParam);
        return list;
    }


    /**
     * 根据id查询轮播图片数据，通过远程调用carousel_service_openfeign接口方法获取数据
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    public ResultUtil info(@PathVariable("id") Integer id) {
        //ResultUtil result = carouselServiceFeign.info(id);
        ResultUtil result = carouselService.getById(id);
        return result;
    }


    /**
     * 修改
     * @param carousel
     * @param session
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Carousel carousel, HttpSession session) {
        carousel.setUpdateUser(Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString()));
        carousel.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //ResultUtil updateResult = carouselServiceFeign.update(carousel);
        ResultUtil updateResult = carouselService.update(carousel);
        return updateResult;
    }


    /**
     * 新增
     * @param carousel
     * @param session
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody Carousel carousel, HttpSession session) {
        Integer loginUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        carousel.setCreateUser(loginUserId);
        carousel.setUpdateUser(loginUserId);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        carousel.setCreateTime(time);
        carousel.setUpdateTime(time);

        //ResultUtil saveResult = carouselServiceFeign.save(carousel);
        ResultUtil saveResult = carouselService.save(carousel);
        return saveResult;
    }


    /**
     * 删除，支持批量删除
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(@RequestBody Integer[] ids, HttpSession session) {
        Integer loginUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        CarouselDeleteRequestParam carouselDeleteRequestParam = new CarouselDeleteRequestParam(ids, loginUserId, time);
        //ResultUtil deleteResult = carouselServiceFeign.delete(carouselDeleteRequestParam);
        ResultUtil deleteResult = carouselService.delete(carouselDeleteRequestParam);
        return deleteResult;
    }
}
